<?php
namespace App\Common\Cla\Com;

use App\Common as com;

class Upload extends com\Cla\ClaBase {

	/**
	 * 获取一个token信息组
	 * @param int $timestamp 时间戳
	 * @return array [ 'timestamp' => {时间戳}, 'token' => {token} ]
	 */
	public static function getTokenInfo( int $timestamp = null ){
		if( is_null( $timestamp ) ) $timestamp = com\Lib\Fun\Time::getTime();
		return [
			'timestamp' => $timestamp,
			'token' => self::createToken( $timestamp ),
		];
	}
	
	/**
	 * 创建一个token
	 * @param int $timestamp 时间戳
	 * @return string token
	 */
	public static function createToken( $timestamp ){
		return md5( com\Cla\Com\Upload\Var_::$config_TokenSalt . $timestamp );
	}
	
	/**
	 * 验证token
	 * @param int $timestamp 时间戳
	 * @param string $token token
	 * @return int 验证结果 [0]不通过 [1]通过
	 */
	public static function checkToken( $timestamp, $token ){
		$sys_token = self::createToken( $timestamp );
		return $token === $sys_token ? 1 : 0;
	}
	
	/**
	 * 检查上传的文件是否存在
	 * @param string|array $filename 文件目录（url），支持字符串(1,2,3)、数组[1,2,3]。如：“/upload/opaccount/generalize/2/7f932e3624858c9886414a9746255794.jpg”
	 * @return int  是否存在 [0]不存在 [1]存在
	 */
	public static function checkUploadFileIsExist( $filenames ){
		$filename_array = com\Lib\Fun\Array_::toArray( $filenames );
		foreach ( $filename_array as $filename_value ){
			$filename_value = str_replace( '/', DIRECTORY_SEPARATOR, $filename_value );
			return file_exists( dirname( com\Cla\Com\Upload\Var_::getUploadRootDir() ) . $filename_value ) ? 1 : 0;
		}
	
	}
	
}
